﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace GMS.BIZ
{
    public class Student
    {
        #region Properties
        private string studentID;
        public string StudentID
        {
            get { return studentID; }
            //set { studentID = value; }
        }
        private string classID;
        public string ClassID
        {
            get { return classID; }
            set { classID = value; }
        }
        private string graduateYear;
        public string GraduateYear
        {
            get { return graduateYear; }
            set { graduateYear = value; }
        }
        private string studentName;
        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }
        private string studentMail;
        public string StudentMail
        {
            get { return studentMail; }
            set { studentMail = value; }
        }
        private string studentGender;
        public string StudentGender
        {
            get { return studentGender; }
            set { studentGender = value; }
        }
        private string studentPolicy;
        public string StudentPolicy
        {
            get { return studentPolicy; }
            set { studentPolicy = value; }
        }
        private string studentHobby;
        public string StudentHobby
        {
            get { return studentHobby; }
            set { studentHobby = value; }
        }
        private string studentClassTeacher;
        public string StudentClassTeacher
        {
            get { return studentClassTeacher; }
            //set { studentClassTeacher = value; }
        }
        private bool editable;
        public bool Editable
        {
            get { return editable; }
            //set { editable = value; }
        }
        private bool ifSLD;
        public bool IfSLD
        {
            get { return ifSLD; }
            //set { ifSLD = value; }
        }
        private string _SLDCompanyName;
        public string SLDCompanyName
        {
            get { return _SLDCompanyName; }
            //set { _SLDCompany = value; }
        }        
        #endregion Properties
        
        public Englishs Englishs;
        public Computers Computers;
        public Jobs Jobs;
        public Companys Companys;
        public Paper Papers;

        /// <summary>
        /// 初始化学生数据
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public Student(string studentID)
        {
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            GMS.ORM.Model.GStudent myGStudent = new GMS.ORM.Model.GStudent();

            myGStudent = optGStudent.GetModel(studentID);
            this.studentID = myGStudent.std_id;
            this.classID = myGStudent.class_id;
            this.graduateYear = myGStudent.graduate_year;
            this.studentName = myGStudent.std_name;
            this.studentMail = myGStudent.std_mail;
            this.studentGender = myGStudent.std_gender;
            this.studentPolicy = myGStudent.std_policy;
            this.studentHobby = myGStudent.std_hobby;

            //获得班主任信息
            GMS.ORM.DAL.UClass optUClass = new GMS.ORM.DAL.UClass();
            this.studentClassTeacher = (optUClass.GetModel(myGStudent.class_id)).class_teacher;
            
            //是否可以修改自己的信息
            GMS.ORM.DAL.GStudentEdit optGStudentEdit = new GMS.ORM.DAL.GStudentEdit();
            if (optGStudentEdit.GetList("std_id='" + this.studentID + "' AND inUse=2").Tables[0].Rows.Count == 1)
                this.editable = true;
            else this.editable = false;

            //是否签署了四联单/四联单的单位
            GMS.ORM.Model.GCompany myGCompany = new GMS.ORM.Model.GCompany();
            GMS.ORM.DAL.GCompany optGCompany = new GMS.ORM.DAL.GCompany();
            DataSet ds = optGCompany.GetList("std_id='" + this.studentID + "' AND ifSLD=1");
            if (ds.Tables[0].Rows.Count == 0) this.ifSLD = false;
            else
            {
                this.ifSLD = true;
                this._SLDCompanyName = ds.Tables[0].Rows[0]["com_name"].ToString();
            }

            //初始化Englishs对象
            GMS.BIZ.Englishs newEnglishs = new Englishs(this.studentID);
            this.Englishs = null;                   //置空Englishs对象
            this.Englishs = newEnglishs;
            //初始化Computers对象
            GMS.BIZ.Computers newComputers = new Computers(this.studentID);
            this.Computers = null;              //置空Computers对象
            this.Computers = newComputers;
            //初始化Jobs对象
            GMS.BIZ.Jobs newJobs = new Jobs(this.studentID);
            this.Jobs = null;                        //置空Jobs对象
            this.Jobs = newJobs;
            //初始化Companys对象
            GMS.BIZ.Companys newCompanys = new Companys(this.studentID);
            this.Companys = null;               //置空Companys对象
            this.Companys = newCompanys;
            //初始化Papers对象
            GMS.BIZ.Paper newPapers = new Paper(this.studentID, this.graduateYear);
            this.Papers = null;
            this.Papers = newPapers;
        }

        /// <summary>
        /// 修改个人信息
        /// </summary>
        public string PersonalInfomationEdit()
        {
            GMS.ORM.Model.GStudent myGStudent = new GMS.ORM.Model.GStudent();
            myGStudent.std_id = this.studentID;
            myGStudent.class_id = this.classID;
            myGStudent.graduate_year = this.graduateYear;
            myGStudent.std_name = this.studentName;
            myGStudent.std_mail = this.studentMail;
            myGStudent.std_gender = this.studentGender;
            myGStudent.std_policy = this.studentPolicy;
            myGStudent.std_hobby = this.studentHobby;

            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            bool ok = optGStudent.Update(myGStudent);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 申请修改信息
        /// </summary>
        public string ApplyEditing()
        {
            GMS.ORM.DAL.GStudentEdit optGStudentEdit = new GMS.ORM.DAL.GStudentEdit();
            if (optGStudentEdit.GetList("std_id='" + this.studentID + "' AND inUse=1").Tables[0].Rows.Count != 0)
                return ErrorMessage.ApplyEditingAlready;

            GMS.ORM.Model.GStudentEdit myGStudentEdit = new GMS.ORM.Model.GStudentEdit();
            myGStudentEdit.std_id = this.studentID;
            myGStudentEdit.inUse = 1;
            myGStudentEdit.editTime = DateTime.Now;
            myGStudentEdit.editcontent = string.Empty;

            bool ok = optGStudentEdit.Add(myGStudentEdit);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 获取我的答辩信息
        /// </summary>
        public DefenseInfo GetMyDefenseInfo()
        {
            DataSet ds = Defense.GetDefenseList(this.studentID);
            int i = ds.Tables[0].Rows.Count;
            if (i == 0)
                return null;
            else if (i > 1)
                return null;
            else
            {
                return Defense.GetDefenseInfo(decimal.Parse(ds.Tables[0].Rows[0]["def_id"].ToString()));
            }
        }

        /// <summary>
        /// 添加或修改我的答辩场次
        /// </summary>
        /// <param name="defenseLibID">答辩场次</param>
        public string AddDefense(decimal defenseLibID)
        {
            GMS.ORM.Model.PDefense myPDefense = new GMS.ORM.Model.PDefense();
            myPDefense.std_id = this.studentID;
            myPDefense.gyear = this.graduateYear;
            myPDefense.pdefense_lib = defenseLibID;

            GMS.ORM.DAL.PDefense optPDefense = new GMS.ORM.DAL.PDefense();
            bool ok = false;
            if (optPDefense.Exists(this.studentID, this.graduateYear))
                ok = optPDefense.Update(myPDefense);
            else
                ok = optPDefense.Add(myPDefense);
            
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 删除我的答辩场次
        /// </summary>
        public string DeleteDefense()
        {
            GMS.ORM.DAL.PDefense optPDefense = new GMS.ORM.DAL.PDefense();
            if (optPDefense.Exists(this.studentID, this.graduateYear))
            {
                bool ok = optPDefense.Delete(this.studentID, this.graduateYear);
                if (!ok) return ErrorMessage.DBOptError;
                else return ErrorMessage.OK;
            }
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 获得学生名字
        /// </summary>
        /// <param name="studentID">学生学号</param>
        static public string GetName(string studentID)
        {
            GMS.ORM.DAL.GStudent optGStudent = new GMS.ORM.DAL.GStudent();
            if (optGStudent.Exists(studentID) == false)
                return ErrorMessage.NoSuchData;
            return optGStudent.GetModel(studentID).std_name;
        }
    }
}
